// pages/thing/thingdetail.js
var QRCode = require('../../utils/weapp-qrcode.js')
var qrcode;

Page({
  mixins: [require('../mixin/themeChanged')],

  /**
   * 页面的初始数据
   */
  data: {
    sn: "",
    thingorder: [{
      "classone": "网络设备",
      "classtwo": "交换机",
      "classthree": "三口",
      "num": "2"
    }, {
      "classone": "网络设备",
      "classtwo": "交换机",
      "classthree": "三口",
      "num": "2"
    }],
    currentid: "",

  },


  changeMark: function (e) {
    var that = this
    var url = wx.getStorageSync("config")
    //得到当前点击数据的id号
    this.setData({
      currentid: e.currentTarget.dataset.id
    })
    console.log(e.currentTarget.dataset.id)


    wx.showModal({
      title: '删除确认',
      content: '请核对删除内容是否正确',
      success: function (res) {
        if (res.confirm) {


          //请求体
          wx.request({
            url: url.service.homeUrl + '/thing/changethingmark',
            data: {
              id: that.data.currentid,
              mark: 0
            },
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            method: 'POST',
            dataType: 'json',
            responseType: 'text',
            success: (res) => {

              wx.showToast({
                title: res.data.msg,
              });

              that.load()

            },
            fail: function (res) {
              wx.showToast({
                title: res.data.msg,
              })
            },
            complete: function (res) {
              wx.showToast({
                title: res.data.msg,
              })
            },
          })




        }
      }
    })
  },


  changeAllMark: function () {
    var that = this
    var url = wx.getStorageSync("config")

    wx.showModal({
      title: '删除确认',
      content: '请核对删除内容是否正确',
      success: function (res) {
        if (res.confirm) {


          wx.request({
            url: url.service.homeUrl + '/thing/changethingallmark',
            data: {
              sn: that.data.sn,
              mark: 0
            },
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            method: 'POST',
            dataType: 'json',
            responseType: 'text',
            success: (res) => {

              wx.showToast({
                title: res.data.msg,
              })
              wx.navigateTo({
                url: '../thing/thingorder',
              })

            },
            fail: function (res) {
              wx.showToast({
                title: res.data.msg,
              })
            },
            complete: function (res) {
              wx.showToast({
                title: res.data.msg,
              })
            },
          })

        }
      }
    })


  },

  changeTheme: function() {
    console.log(this.data);
    getApp().themeChanged(this.data.theme === 'light' ? 'dark' : 'light');
},



  // 长按保存
  save: function () {
    console.log('save')
    wx.showActionSheet({
      itemList: ['保存图片'],
      success: function (res) {
        console.log(res.tapIndex)
        if (res.tapIndex == 0) {
          qrcode.exportImage(function (path) {
            wx.saveImageToPhotosAlbum({
              filePath: path,
            })
          })
        }
      }
    })
  },

  load: function () {
    var that = this
    var url = wx.getStorageSync("config")


    wx.request({
      url: url.service.homeUrl + '/thing/getthingorderdetail',
      data: {
        sn: that.data.sn
      },
      method: 'GET',
      dataType: 'json',
      responseType: 'text',
      success: (res) => {

        that.setData({
          thingorder: res.data,
        })


      },
      fail: function (res) {},
      complete: function (res) {},
    })


  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this

    this.setData({
      sn: options.sn
    })

    that.load()

    qrcode = new QRCode('canvas', {
      // usingIn: this,
      text: that.data.sn,
      image: '',
      width: 150,
      height: 150,
      colorDark: "#000000",
      colorLight: "white",
      correctLevel: QRCode.CorrectLevel.H,
    });


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    var sn = this.data.sn
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    return {
      title: '移动办公平台领用码',
      path: 'pages/thing/thingdetail?sn='+sn
    }
  }
  })